package com.loanapp.base.controller.aop;

import com.loanapp.base.common.dto.ResponseDto;
import com.loanapp.base.common.exception.BizExceptionEnum;
import com.loanapp.base.common.exception.BusinessException;
import com.loanapp.base.common.exception.RequestException;
import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.UndeclaredThrowableException;

/**
 * 全局的的异常拦截器（拦截所有的控制器）（带有@RequestMapping注解的方法上都会拦截）
 *
 * @author fengshuonan
 * @date 2016年11月12日 下午3:19:56
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 拦截业务异常
     *
     * @author fengshuonan
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public ResponseDto notFount(BusinessException e) {
        e.printStackTrace();
        return ResponseDto.build(e.getCode(),e.getMessage());
    }
    /**
     * 拦截未知的运行时异常
     *
     * @author fengshuonan
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResponseDto notFount(RuntimeException e) {
        e.printStackTrace();
        return  ResponseDto.build(BizExceptionEnum.SYSTEM_EEROR);
    }

}
